package com.visikard.ui.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.MerchantGetDashBoardInfoTask;
import com.visikard.item.ItemMerchantHistory;
import com.visikard.item.MerchantNewTradeItem;
import com.visikard.item.MerchantPeopleNearByItem;
import com.visikard.system.Device;
import com.visikard.ui.R;
import com.visikard.ui.adapter.MerchantHistoryAdapter;
import com.visikard.ui.adapter.MerchantNewTradeAdapter;
import com.visikard.ui.adapter.MerchantPeopleNearByAdapter;
import com.visikard.utility.Utils;
import com.visikard.widget.UnscrollGallery;
import com.visikard.widget.VerticalListView;

public class MerchantHomeFragment extends BaseFragment {

	// private static final int MENU_GIVE_GIFT_KARD = 0;
	// private static final int MENU_GIVE_POINT = 1;
	// private static final int MENU_GIVE_COUPON = 2;
	// private static final int MENU_GIVE_PRODUCT = 3;

	private TextView txtTotalAudiences, txtTotalCampaigns, txtTotalPoints, txtTotalCoupons, txtTotalGiftKards;
	private VerticalListView listTimeLine;
	private UnscrollGallery listViewPeople;
	private UnscrollGallery listViewNewTrade;
	private View listViewHeader;

	private MerchantHistoryAdapter mHistoryAdapter;
	private MerchantPeopleNearByAdapter mPeopleNearByAdapter;
	private MerchantNewTradeAdapter mNewTradeAdapter;

	ViewGroup mContainer;
	PopupWindow mPopupMenu;

	BaseTask mAsyncTask;

	@SuppressWarnings("deprecation")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.merchant_home_fragment, container, false);
		mContainer = container;

		listTimeLine = (VerticalListView) view.findViewById(R.id.home_screen_listview);

		listViewHeader = inflater.inflate(R.layout.merchant_home_header, listTimeLine, false);
		Utils.overrideFonts(listViewHeader);
		txtTotalAudiences = (TextView) listViewHeader.findViewById(R.id.txt_audience);
		txtTotalCampaigns = (TextView) listViewHeader.findViewById(R.id.txt_campaign);
		txtTotalPoints = (TextView) listViewHeader.findViewById(R.id.txt_points);
		txtTotalCoupons = (TextView) listViewHeader.findViewById(R.id.txt_coupons);
		txtTotalGiftKards = (TextView) listViewHeader.findViewById(R.id.txt_giftkards);

		listViewPeople = (UnscrollGallery) listViewHeader.findViewById(R.id.people_nearby);
		listViewPeople.setCallbackDuringFling(true);
		MarginLayoutParams mlp = (MarginLayoutParams) listViewPeople.getLayoutParams();
		int leftMargin = (Device.getDeviceWidth() * 4) / 5;
		mlp.setMargins(-leftMargin, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);

		listViewNewTrade = (UnscrollGallery) listViewHeader.findViewById(R.id.new_trade);
		listViewNewTrade.setCallbackDuringFling(true);
		MarginLayoutParams mlp2 = (MarginLayoutParams) listViewNewTrade.getLayoutParams();
		int leftMargin2 = (Device.getDeviceWidth() * 4) / 5;
		mlp2.setMargins(-leftMargin2, mlp2.topMargin, mlp2.rightMargin, mlp2.bottomMargin);

		listTimeLine.addHeaderView(listViewHeader);
		listTimeLine.setHeaderDividersEnabled(false);

		mHistoryAdapter = new MerchantHistoryAdapter(getActivity(), new ArrayList<ItemMerchantHistory>());
		listTimeLine.setAdapter(mHistoryAdapter);

		mPeopleNearByAdapter = new MerchantPeopleNearByAdapter(getActivity(), new ArrayList<MerchantPeopleNearByItem>());
		listViewPeople.setAdapter(mPeopleNearByAdapter);

		mNewTradeAdapter = new MerchantNewTradeAdapter(getActivity(), new ArrayList<MerchantNewTradeItem>());
		listViewNewTrade.setAdapter(mNewTradeAdapter);

		// dummy data
		for (int i = 0; i < 10; i++) {
			ItemMerchantHistory itemHistory = new ItemMerchantHistory();
			itemHistory.setContent("Content " + i);
			itemHistory.setDesc("Desc " + i);
			itemHistory.setName("Name " + i);
			itemHistory.setImage("");
			itemHistory.setDateTime(System.currentTimeMillis());
			mHistoryAdapter.add(itemHistory);

			mPeopleNearByAdapter.add(new MerchantPeopleNearByItem());
			mNewTradeAdapter.add(new MerchantNewTradeItem());
		}

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		mAsyncTask = new MerchantGetDashBoardInfoTask(mHandler);
		mAsyncTask.execute(VKRegApplication.merchantAccountObj.getIdUsers(),
				VKRegApplication.merchantAccountObj.getIdKard());
	}

	private void doGetMerchantDashBoardSuccess(Bundle data) {
		long totalAudiences = data.getLong(UIMessage.KEY_TOTAL_AUDIENCES);
		long totalCampaigns = data.getLong(UIMessage.KEY_TOTAL_CAMPAIGNS);
		long totalPoints = data.getLong(UIMessage.KEY_TOTAL_POINTS);
		long totalCoupons = data.getLong(UIMessage.KEY_TOTAL_COUPONS);
		long totalGiftKards = data.getLong(UIMessage.KEY_TOTAL_GIFTKARDS);

		txtTotalAudiences.setText(String.valueOf(totalAudiences));
		txtTotalCampaigns.setText(String.valueOf(totalCampaigns));
		txtTotalPoints.setText(String.valueOf(totalPoints));
		txtTotalCoupons.setText(String.valueOf(totalCoupons));
		txtTotalGiftKards.setText(String.valueOf(totalGiftKards));
	}

	@Override
	protected boolean handleMessage(Message msg) {
		switch (msg.what) {
			case UIMessage.MSG_MERCHANT_GET_DASHBOARD_INFO_SUCCESS:
				doGetMerchantDashBoardSuccess(msg.getData());
				return true;
		}
		return super.handleMessage(msg);
	}

	// private PopupWindow createPopupMenu() {
	// LayoutInflater layoutInf = LayoutInflater.from(getActivity());
	// // create root view
	// View popupRootView = layoutInf.inflate(R.layout.merchant_home_popup_menu, mContainer, false);
	// LinearLayout contentView = (LinearLayout) popupRootView.findViewById(R.id.content_container);
	//
	// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
	// LayoutParams.MATCH_PARENT, 1);
	// // Give gift kard menu item
	// TextView txtMenuItem = (TextView) layoutInf.inflate(R.layout.merchant_home_menu_item, mContainer, false);
	// txtMenuItem.setText(getString(R.string.menu_give_gift_kard));
	// txtMenuItem.setOnClickListener(new View.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// doMenuItemClicked(MENU_GIVE_GIFT_KARD);
	// }
	// });
	// txtMenuItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.line_bottom);
	// contentView.addView(txtMenuItem, params);
	// // Give point menu item
	// txtMenuItem = (TextView) layoutInf.inflate(R.layout.merchant_home_menu_item, mContainer, false);
	// txtMenuItem.setText(getString(R.string.menu_give_point));
	// txtMenuItem.setOnClickListener(new View.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// doMenuItemClicked(MENU_GIVE_POINT);
	// }
	// });
	// txtMenuItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.line_bottom);
	// contentView.addView(txtMenuItem, params);
	// // Give coupon menu item
	// txtMenuItem = (TextView) layoutInf.inflate(R.layout.merchant_home_menu_item, mContainer, false);
	// txtMenuItem.setText(getString(R.string.menu_give_coupon));
	// txtMenuItem.setOnClickListener(new View.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// doMenuItemClicked(MENU_GIVE_COUPON);
	// }
	// });
	// txtMenuItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.line_bottom);
	// contentView.addView(txtMenuItem, params);
	// // Give product menu item
	// txtMenuItem = (TextView) layoutInf.inflate(R.layout.merchant_home_menu_item, mContainer, false);
	// txtMenuItem.setText(getString(R.string.menu_give_product));
	// txtMenuItem.setOnClickListener(new View.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// doMenuItemClicked(MENU_GIVE_PRODUCT);
	// }
	// });
	// contentView.addView(txtMenuItem, params);
	//
	// // create popup window and return
	// PopupWindow popupWindow = new PopupWindow(getActivity());
	// popupWindow.setContentView(popupRootView);
	// popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
	// // Removes default black background
	// popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(color.transparent)));
	// // Closes the popup window when touch outside of it - when looses focus
	// popupWindow.setOutsideTouchable(true);
	// popupWindow.setFocusable(true);
	// return popupWindow;
	// }

	// private void doMenuItemClicked(int menuId) {
	// switch (menuId) {
	// case MENU_GIVE_GIFT_KARD:
	// mPopupMenu.dismiss();
	// break;
	// case MENU_GIVE_POINT:
	// mPopupMenu.dismiss();
	// break;
	// case MENU_GIVE_COUPON:
	// mPopupMenu.dismiss();
	// break;
	// case MENU_GIVE_PRODUCT:
	// mPopupMenu.dismiss();
	// break;
	// }
	// }

}
